/**
 * 
 */
package edu.arizona.eller.mis.liferoulette.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import edu.arizona.eller.mis.liferoulette.entity.ActivityRating;

/**
 * @author Blaine Boule - blaineb@email.arizona.edu
 *
 */
public class ActivityRatingDAO extends BaseDAO {

	public static final String _ACTIVITY_RATING_TABLE = "ACTIVITY_RATING";
	public static final String _CREATE_STATEMENT = "CREATE TABLE " + _ACTIVITY_RATING_TABLE  +" (id INTEGER PRIMARY KEY AUTOINCREMENT, activityId INTEGER REFERENCES ACTIVITY(id), rating integer not null, comments text, activity_date text)";
	private String[] columnArray = { ActivityRating._id, ActivityRating._activityId, ActivityRating._rating, ActivityRating._comments, ActivityRating._date };
	
	public ActivityRatingDAO(Context context) {
		super(context);
	}
	
	public List<ActivityRating> getActivitieRatings(Integer activityId) {
		List<ActivityRating> list = new ArrayList<ActivityRating>();
		Cursor cursor = db.query(_ACTIVITY_RATING_TABLE, columnArray,  ActivityRating._activityId + "=" + activityId, null, null, null, null);
		
		if(cursor.moveToFirst()){
			
			do{
				ActivityRating ar = new ActivityRating(cursor.getInt(0), cursor.getInt(1), cursor.getInt(2),cursor.getString(3),cursor.getString(4));
				list.add(ar);
			}while(cursor.moveToNext());
			
			if(cursor!=null && !cursor.isClosed()){
				cursor.close();
			}
		}
		return list;
	}
	
	public ActivityRating findById(Integer id){
		ActivityRating ar = null;
		Cursor cursor = db.query(_ACTIVITY_RATING_TABLE, columnArray,  ActivityRating._id + "=" + id, null, null, null, null);
		
		if(cursor.moveToFirst()){
			ar = new ActivityRating(cursor.getInt(0), cursor.getInt(1), cursor.getInt(2),cursor.getString(3),cursor.getString(4));
		}
		
		return ar;
	}
	
	public int update(ActivityRating ar){
		ContentValues update = new ContentValues();
		update.put(ActivityRating._activityId, ar.getActivityId());
		update.put(ActivityRating._rating, ar.getRating());
		update.put(ActivityRating._comments, ar.getComments());
		
		return db.update(_ACTIVITY_RATING_TABLE, update, ActivityRating._id + "=" + ar.getId(), null);
	}
	
	public long insert(ActivityRating ar) {
	
		ContentValues insert = new ContentValues();
		insert.put(ActivityRating._activityId, ar.getActivityId());
		insert.put(ActivityRating._rating, ar.getRating());
		insert.put(ActivityRating._comments, ar.getComments());
		
		return db.insert(_ACTIVITY_RATING_TABLE, null, insert);	
	}
	
	public int delete(ActivityRating ar){
		
		return db.delete(_ACTIVITY_RATING_TABLE, ActivityRating._id + "=" + ar.getId(), null);
	}

}
